package com.huika.renrench.control.investment.activity;

import java.util.ArrayList;

import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.android.volley.Response.Listener;
import com.android.volley.VolleyError;
import com.google.gson.reflect.TypeToken;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.huika.renrench.GlobalApp;
import com.huika.renrench.R;
import com.huika.renrench.config.UrlConstants;
import com.huika.renrench.control.common.base.BaseAct;
import com.huika.renrench.control.investment.adapter.InvestmentGoAdapter;
import com.huika.renrench.http.FormResultRequest;
import com.huika.renrench.http.JSONWrapAjaxParams;
import com.huika.renrench.http.RequestResult;
import com.huika.renrench.support.bean.BorrowDetailBean;
import com.huika.renrench.view.TitleBarHelper;

/**
 * 
 * @description：投资去向
 * @author zc
 * @date 2015年6月2日 上午10:27:40
 */
public class InvestmentGoActivity extends BaseAct implements Listener<RequestResult<ArrayList<BorrowDetailBean>>>, OnRefreshListener2<ListView>, OnItemClickListener {

	private PullToRefreshListView invest_go;
	private boolean isUpRefresh;
	private ArrayList<BorrowDetailBean> beanList = new ArrayList<BorrowDetailBean>();
	private InvestmentGoAdapter adapter;
	private int pageNo = 1;
	private int pageSize = 10;
	private String userId;
	
	@Override
	public void setRootView() {
		setContentView(R.layout.activity_investment_go);
	}
	
	@Override
	protected void initWidget() {
		super.initWidget();
		invest_go = (PullToRefreshListView) findViewById(R.id.invest_go);
		invest_go.setOnRefreshListener(this);
		invest_go.setOnItemClickListener(this);
		adapter = new InvestmentGoAdapter(this);
		invest_go.setAdapter(adapter);
		getLoanProjectList(userId, pageNo, pageSize);
	}
	
	@Override
	protected void initData() {
		super.initData();
		TitleBarHelper barHelper = new TitleBarHelper(this, -1);
		barHelper.setTitleMsg("投资款去向");
		barHelper.setLeftMsg("投资");
		userId = GlobalApp.getInstance().getUserId();
	}
	
	private void getLoanProjectList(String userId, int pageNo, int pageSize) {
		showLoadingDialog(getString(R.string.common_loading));
		JSONWrapAjaxParams ajaxParams = new JSONWrapAjaxParams();
		ajaxParams.putCommonTypeParam("userId", userId);
		ajaxParams.putCommonTypeParam("pageNo", pageNo);
		ajaxParams.putCommonTypeParam("pageSize", pageSize);
		FormResultRequest<ArrayList<BorrowDetailBean>> request = new FormResultRequest<ArrayList<BorrowDetailBean>>(UrlConstants.GETLOANPROJECTLIST, this, this, new TypeToken<RequestResult<ArrayList<BorrowDetailBean>>>() {
		}.getType());
		request.setRequestParams(ajaxParams);
		executeRequest(request);
	}

	@Override
	public void onPullDownToRefresh(PullToRefreshBase<ListView> arg0) {
		pageNo = 1;
		getLoanProjectList(userId, pageNo, pageSize);
	}

	@Override
	public void onPullUpToRefresh(PullToRefreshBase<ListView> arg0) {
		isUpRefresh = true;
		getLoanProjectList(userId, ++pageNo, pageSize);
	}
	
	@Override
	public void onResponse(RequestResult<ArrayList<BorrowDetailBean>> arg0) {
		invest_go.onRefreshComplete();
		dismissLoadingDialog();
		if(arg0.flag == 1) {
			if(arg0.getRs() != null && arg0.getRs().size() == 10) {
				invest_go.setMode(Mode.BOTH);
			} else {
				invest_go.setMode(Mode.PULL_FROM_START);
			}
			if(isUpRefresh) {
				beanList.addAll(arg0.getRs());
				adapter.setGroup(beanList);
			} else {
				beanList = arg0.getRs();
				adapter.setGroup(beanList);
			}
		}
		isUpRefresh = false;
	}
	
	@Override
	public void onErrorResponse(VolleyError error) {
		super.onErrorResponse(error);
		invest_go.onRefreshComplete();
		if(isUpRefresh) {
			pageNo--;
		}
		isUpRefresh = false;
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
		BorrowDetailBean bean = beanList.get(position - 1);
		Intent intent = new Intent(this, BorrowDetailActivity.class);
		intent.putExtra("loanId", bean.getLoanId());
		startActivity(intent);
	}

}
